package ro.gateway.aida.locations;

import java.sql.SQLException;
import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.CRegion;
import ro.gateway.aida.obj.Country;
import ro.gateway.aida.obj.db.AIDALocationDB;
import ro.gateway.aida.obj.db.CountyDB;
import ro.gateway.aida.obj.db.RegionDB;
import ro.gateway.aida.obj.db.TownDB;
import ro.gateway.aida.obj.db.VillageDB;
import ro.gateway.aida.servlet.Constants;
import ro.gateway.aida.utils.HttpUtils;


/**
 * <p>Title: Romanian AIDA</p> <p>Description: </p> <p>Copyright: Copyright (comparator) 2003</p> <p>Company:
 * ro-gateway</p>
 *
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: BrowserAction.java,v 1.1 2004/10/24 23:37:41 mihaipostelnicu Exp $
 */
public class BrowserAction extends Action {
     public ActionForward execute( ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response )
        throws Exception {
          ServletContext application = this.getServlet().getServletContext();
          PersistenceToken token = PersistenceManager.
             tokenLookup( application );
          Hashtable services = ( Hashtable ) application.
             getAttribute( Constants.SERVICES );

          LocationForm location_form = ( LocationForm ) form;
          location_form.clear_objects();

          long region_id = location_form.getRegion_id();
          long county_id = location_form.getCounty_id();
          long town_id = location_form.getTown_id();
          long village_id = location_form.getVillage_id();

          location_form.action = HttpUtils.getValidTrimedString( request, "action", null );
          if ( ( location_form.country_iso3 != null ) && ( location_form.country_iso3.length() < 1 ) ) {
               location_form.country_iso3 = null;
          }

          if ( "save".equals( location_form.action ) ) {
               if ( "-1".equals( location_form.getCountry_iso3() ) ) {
                    location_form.setCountry_iso3( null );
               }
               return mapping.findForward( "finnish" );
          } else if ( "cancel".equals( location_form.action ) ) {
               location_form.clear_objects();
               return mapping.findForward( "finnish" );
          } else if ( "new".equals( location_form.action ) ) {
               location_form.reset_data();
               location_form.block_country = HttpUtils.
                  getValidTrimedString( request, "block_country", null );
               int block_cregion_id = HttpUtils.
                  getInt( request, "block_cregion", -1 );
               if ( block_cregion_id != -1 ) {
                    location_form.block_cregion = String.valueOf( block_cregion_id );
               }
          }

          while ( true ) {
               try {
                    // country regions
                    location_form.setCregions( LocationEditorAction.make_tree_list( AIDALocationDB
                       .getManager( token ).getRootCregions( true ) ) );
                    if ( location_form.getCregions() != null ) {
                         request.setAttribute( "cregions", location_form.cregions );
                    }
                    if ( location_form.getCregion_id() != -1 ) {
                         location_form.setCregion( AIDALocationDB.getManager( token ).
                            getCRegionById( location_form.cregion_id ) );
                    } else {
                         if ( location_form.block_cregion != null ) {
                              int block_cregion_id = HttpUtils.getInt( location_form.block_cregion, -1 );
                              int i = 0;
                              for ( ; i < location_form.cregions.length; i++ ) {
                                   if ( location_form.cregions[ i ].getId() == block_cregion_id ) {
                                        break;
                                   }
                              }
                              if ( i < location_form.cregions.length ) {
                                   location_form.cregions = new CRegion[]{
                                        location_form.cregions[ i ]
                                   };
                                   request.setAttribute( "cregions", location_form.cregions );
                              }
                         }
                         break;
                    }

//                    System.out.println( "block:" + location_form.block_country );
                    // countries
                    if ( location_form.block_country == null ) {

                         location_form.setCountries( AIDALocationDB.getManager( token )
                            .getCountriesByCRegion( location_form.cregion_id ) );
//                                    .getAllCountries() );
                         if ( location_form.countries != null ) {
                              request.setAttribute( "countries", location_form.countries );
                         }
                    } else {
                         location_form.setCountries( new Country[]{
                              AIDALocationDB.getManager( token ).getCountryByISO3( location_form.block_country )
                         } );
                         if ( location_form.countries != null ) {
                              request.setAttribute( "countries", location_form.countries );
                         }
                    }
                    if ( location_form.country_iso3 == null ) {
                         break;
                    }

                    // country
                    location_form.country = AIDALocationDB.getManager( token ).
                       getCountryByISO3( location_form.country_iso3 );
                    if ( location_form.country != null ) {
                         request.setAttribute( "country", location_form.country );
                    }
                    if ( ( location_form.countries == null ) || location_form.country_iso3 == null ) {
                         break;
                    }

                    if ( location_form.country != null
                       && location_form.block_country != null ) {
                         location_form.cregions = new CRegion[]{
                              location_form.cregion
                         };
                    }

                    // regions
                    location_form.regions = RegionDB.getManager( token ).
                       getAllByCountry( location_form.country_iso3 );
                    if ( location_form.regions != null ) {
                         request.setAttribute( "regions", location_form.regions );
                    }
                    if ( ( location_form.regions == null ) || region_id == -1 || ( "country".equals( location_form.field_changed ) ) ) {
                         break;
                    }

                    location_form.region = RegionDB.getManager( token ).
                       getById( ( int ) location_form.region_id );
                    if ( location_form.region == null ) {
                         break;
                    }

                    // counties
                    location_form.counties = CountyDB.getManager( token ).
                       getForRegion( ( int ) region_id );
                    if ( location_form.counties != null ) {
                         request.setAttribute( "counties", location_form.counties );
                    }
                    if ( ( location_form.counties == null ) || county_id == -1 || ( "region".equals( location_form.field_changed ) ) ) {
                         break;
                    }
                    location_form.county = CountyDB.getManager( token ).
                       getById( ( int ) location_form.county_id );
                    if ( location_form.county == null ) {
                         break;
                    }

                    // towns
                    location_form.towns = TownDB.getManager( token ).
                       getAllForCounty( ( int ) county_id );
                    if ( location_form.towns != null ) {
                         request.setAttribute( "towns", location_form.towns );
                    }
                    if ( ( location_form.towns == null ) || town_id == -1 || ( "county".equals( location_form.field_changed ) ) ) {
                         break;
                    }
                    location_form.town = TownDB.getManager( token ).
                       getById( ( int ) location_form.town_id );
                    if ( location_form.town == null ) {
                         break;
                    }

                    // villages
                    location_form.villages = VillageDB.getManager( token ).
                       getAllForTown( ( int ) town_id );
                    if ( location_form.villages != null ) {
                         request.setAttribute( "villages", location_form.villages );
                    }
                    break;
               } catch ( SQLException sqlEx ) {
                    sqlEx.printStackTrace();
               }
          }

          request.setAttribute( "marked_field", location_form.getMarkedField() );

          return mapping.findForward( "browser_jsp" );
     }
}
